package com.topsec.erp.controller;

import com.topsec.erp.entity.Salary;
import com.topsec.erp.service.SalaryService;
import com.topsec.erp.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * author: zsl
 * date: 2019/11/22 11:13
 * Description:
 */
@Controller
public class SalaryController {
    // 日志记录器
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SalaryService salaryService;


    @RequestMapping(value="/salary/updatePage",method= RequestMethod.GET)
    public String update(Model model, Long id){
        Salary byId = salaryService.findById(id);
        model.addAttribute("user",byId);

        return "/salary/add";
    }

    @RequestMapping(value="/salary/delete",method=RequestMethod.GET)
    public void delete(Integer id){
        System.out.println(id);
        if(id!=null){
            salaryService.delete(id);
        }
    }

    @RequestMapping(value="/salary/add",method=RequestMethod.POST)
    public String add(@RequestParam("id") Long id,
                      @RequestParam("username") String username,
                      @RequestParam("month") String month,
                      @RequestParam("baseSalary") String baseSalary,
                      @RequestParam("jobSalary") String jobSalary,
                      @RequestParam("foodSalary") String foodSalary,
                      @RequestParam("allSalary") String allSalary     ){
        Salary user = new Salary();
        user.setUsername(username);
        user.setMonth(month);
        user.setBaseSalary(baseSalary);
        user.setJobSalary(jobSalary);
        user.setFoodSalary(foodSalary);
        user.setAllSalary(allSalary);
        if(id != null){
            user.setId(id);
        }
        salaryService.save(user);

        return "redirect:/salary/list";
    }


    @RequestMapping(value="/salary/addpage")
    public String add(){
//        if(id!=null){
//            Salary job = rainservice.get_UserInfo(id);
//            model.addAttribute("job",job);
//        }
        return "/salary/add";
    }


    // 如果在目录下输入任何不存在的参数，则跳转到list
    @RequestMapping(value="/salary/list")
    public ModelAndView index2(Model model){
        List<Salary> users = salaryService.queryAll();
        model.addAttribute("list",users);
        ModelAndView mav = new ModelAndView("/salary/list");
        mav.addObject("list", users);
        return mav;

//        return "/salary/list";
    }

    // 如果在目录下输入任何不存在的参数，则跳转到list
    @RequestMapping(value="/salary/search")
    public ModelAndView   search(Model model, @RequestParam("username") String username){
        List<Salary> users = salaryService.findByUsername(username);
        model.addAttribute("list",users);
        ModelAndView mav = new ModelAndView("/salary/list");
        mav.addObject("list", users);
        return mav;
    }

    /**
     * 查询全部员工
     */
    @GetMapping("/salary/query")
    public String queryAll(){
        List<Salary> all = salaryService.queryAll();
        return JsonUtil.json(all);
    }

    /**
     * 单个修改
     */
    @RequestMapping("/salary/update")
    public String updateUser(@RequestBody Salary user){

        String   result = "success";
        try {
            salaryService.save(user);
        } catch (Exception e) {
            result = "false";
            e.printStackTrace();
        }

        return result;
    }

}
